#pragma once

#include "Noncopyable.h"
#include "Timestamp.h"

#include <vector>
#include <unordered_map>
          
class Channel;
class EventLoop;

// muduo库中多路事件分发器的核心IO复用模块,epoll_wait
class Poller : noncopyable
{
public:
    using ChannelList = std::vector<Channel*>;

    Poller(EventLoop* loop);
    virtual ~Poller() = default;

    // 给所有的IO复用保留同一的接口
    virtual Timestamp poll(int timeoutMs,ChannelList* activeChannels) = 0;
    virtual void updateChannel(Channel* channel) = 0;
    virtual void removeChannel(Channel* channel) = 0;
    
    // 判断参数channel是否再当前Poller当中
    bool hasChannele(Channel* channel) const;

    // EventLoop可以通过接口获取默认的IO复用的具体实现（不用在EventLoop中实现用epoll还是poll）
    static Poller* newDefaultPoller(EventLoop* loop);
protected:
    // map的key: sockfd  value:sockfd所属的Channel通道类型
    using ChannelMap = std::unordered_map<int,Channel*>;
    ChannelMap  channels_;
private:
    EventLoop* ownerLoop_; // 定义Poller所属的事件循环EeventLoop
};